﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.Data.Services;

namespace MK.oDataConsuming.Web.Model {
    public class CarContext : IUpdatable {

        #region Members
        private static List<Car> _cars;
        #endregion

        #region Constructor
        public CarContext() {
            if (_cars == null) {
                _cars = new List<Car>();
            }
        }
        #endregion

        #region Properties
        public IQueryable<Car> Cars {
            get {
                return _cars.AsQueryable<Car>();
            }
        }
        #endregion

        #region IUpdatable Methods
        public void AddReferenceToCollection(object targetResource, string propertyName, object resourceToBeAdded) {
            throw new NotImplementedException();
        }

        public void ClearChanges() {
            throw new NotImplementedException();
        }

        public object CreateResource(string containerName, string fullTypeName) {

            Car car = new Car();

            int tmpId = 0;
            if (_cars.Count > 0) {
                tmpId = _cars.Max(c => c.Id);
            }

            car.Id = ++tmpId;
            return car;
        }

        public void DeleteResource(object targetResource) {
            List<Car> carsToDelete = (List<Car>)targetResource;
            Car carToDelete = carsToDelete[0];
            _cars.Remove(carToDelete);
        }

        public object GetResource(IQueryable query, string fullTypeName) {
            List<Car> carList = query.Cast<Car>().ToList();
            return carList;
        }

        public object GetValue(object targetResource, string propertyName) {
            throw new NotImplementedException();
        }

        public void RemoveReferenceFromCollection(object targetResource, string propertyName, object resourceToBeRemoved) {
            //throw new NotImplementedException();
        }

        public object ResetResource(object resource) {
            throw new NotImplementedException();
        }

        public object ResolveResource(object resource) {
            if (resource is Car) {
                _cars.Add((Car)resource);
            }
            return resource;
        }

        public void SaveChanges() {
            //We don't have any database/file to store the data in this test.            
        }

        public void SetReference(object targetResource, string propertyName, object propertyValue) {
            throw new NotImplementedException();
        }

        public void SetValue(object targetResource, string propertyName, object propertyValue) {
            if (propertyName == "Manufacturer") {
                ((Car)targetResource).Manufacturer = (string)propertyValue;
            } else if (propertyName == "ProductionYear") {
                ((Car)targetResource).ProductionYear = (string)propertyValue;
            } else if (propertyName == "Id") {
                //We do nothing here because id is already set.
            } else {
                throw new Exception("Property not implemented.");
            }
        }
        #endregion
    }
}